<template>
  <div class="order">
    <div class="order-title">
      <van-icon @click="goBack" size="24" name="arrow-left" />
      <div v-if="!flag">{{ data }}订单</div>
      <div v-if="flag">{{ oname }}订单</div>
      <van-icon size="24" name="search" />
    </div>
    <div class="order-tab">
      <van-tabs v-model:active="index" sticky animated title-active-color="red" line-height="1px" color="#ed5b00"
        @click-tab="getName">
        <van-tab title="全部">
          <div class="ct">
            <div class="item" v-for="item in totalOrder" :key="item.price">
              <div class="ctBox">
                <div class="pic">
                  <img :src="item.url">
                </div>
                <div class="desc">
                  <div class="name">{{ item.name }} {{ item.level }} {{ item.color }}</div>
                  <div class="price"> ￥{{ item.price * item.number }}</div>
                  <div class="number">×{{ item.number }}</div>
                </div>
              </div>
              <div class="ctBtn">
                <span class="liji">查看详情</span><span class="cancel" @click="deleteOrder(item)">删除订单</span>
              </div>
            </div>
          </div>
          <div class="order-all" v-if="totalOrder.length == 0">
            <img src="https://s1.mi.com/m/images/m/empty.png" alt="">
            <div style="text-align: center;">您还没有 订单</div>
          </div>
          <RecommendView></RecommendView>
        </van-tab>
        <van-tab title="待付款">

          <div class="ct">
            <div class="item" v-for="item in orderLoding" :key="item.price">
              <div class="ctBox">
                <div class="pic">
                  <img :src="item.url">
                </div>
                <div class="desc">
                  <div class="name">{{ item.name }} {{ item.level }} {{ item.color }}</div>
                  <div class="price"> ￥{{ item.price * item.number }}</div>
                  <div class="number">×{{ item.number }}</div>
                </div>
              </div>
              <div class="ctBtn">
                <span class="liji" @click="goPay(item)">立即付款</span><span class="cancel" @click="cancelOrder(item)">取消订单</span>
              </div>
            </div>
          </div>
          <div class="order-all" v-if="orderLoding.length == 0">
            <img src="https://s1.mi.com/m/images/m/empty.png" alt="">
            <div style="text-align: center;">您还没有 待付款 订单</div>
          </div>
          <RecommendView></RecommendView>
        </van-tab>
        <van-tab title="待收货">

          <div class="ct">
            <div class="item" v-for="item in overPay" :key="item.price">
              <div class="ctBox">
                <div class="pic">
                  <img :src="item.url">
                </div>
                <div class="desc">
                  <div class="status">买家已付款</div>
                  <div class="name">{{ item.name }} {{ item.level }} {{ item.color }}</div>
                  <div class="price"> ￥{{ item.price * item.number }}</div>
                  <div class="number">×{{ item.number }}</div>
                </div>
              </div>
              <div class="ctBtn">
                <span class="liji" @click="returnOrder(item)">退货</span><span class="cancel">修改地址</span>
              </div>
            </div>
          </div>
          <div class="order-all" v-if="overPay.length == 0">
            <img src="https://s1.mi.com/m/images/m/empty.png" alt="">
            <div style="text-align: center;">您还没有 待收货 订单</div>
          </div>
          <RecommendView></RecommendView>
        </van-tab>
      </van-tabs>

    </div>
  </div>
</template>
<script setup lang="ts">
import { ref, onMounted } from "vue";
import { useRoute,useRouter } from 'vue-router'
import { SubmitOrder } from '@/stores/total'
import { type cart } from '@/typings'
import { Dialog, Snackbar } from '@varlet/ui'



const orderDesc = SubmitOrder()
const orderLoding = ref<Array<cart>>([])
orderLoding.value = orderDesc.payLoding

const totalOrder = ref<Array<cart>>([])

const overPay = ref<Array<cart>>([])
  
overPay.value = orderDesc.overPay


orderDesc.setTotalOrder()
totalOrder.value = orderDesc.totalOrder



const data = ref('');
const route = useRoute();
const index = ref();
const oname = ref();
const flag = ref(true);

const goBack = () => {
  window.history.back();
};

const getName = (title: string) => {
  flag.value = false;
  data.value = title.title
  return data.value
}
onMounted(() => {
  index.value = Number(route.query?.index)
  oname.value = route.query?.name
})

const itemValue = ref<Array<cart>>([])
let cancelArr: { [key: string]: () => void } = {
  confirm: () => {
    orderDesc.cancelOrder(itemValue.value)
    orderDesc.setTotalOrder()
    totalOrder.value = orderDesc.totalOrder
    Snackbar.success('取消成功')

  },
  cancel: () => Snackbar.error('继续保留'),
};
const cancelOrder = async (item: cart) => {
  itemValue.value = []

  itemValue.value.push(item)


  cancelArr[await Dialog('是否要取消订单')]();

}



let deleteArr: { [key: string]: () => void } = {
  confirm: () => {
    orderDesc.deleteOrder(itemValue.value)
    orderDesc.setTotalOrder()
    totalOrder.value = orderDesc.totalOrder
    Snackbar.success('删除成功')
  },
  cancel: () => Snackbar.error('继续保留'),
};
const deleteOrder = async (item: cart) => {
  itemValue.value = []

  itemValue.value.push(item)
  deleteArr[await Dialog('是否要删除订单')]();

}
let returnArr: { [key: string]: () => void } = {
  confirm: () => {

    orderDesc.returnOrder(itemValue.value)
    orderDesc.setTotalOrder()
    totalOrder.value = orderDesc.totalOrder
    Snackbar.success('退货成功')
  },
  cancel: () => Snackbar.error('继续保留'),
};


const returnOrder = async (item: cart) => {
  itemValue.value = []
  itemValue.value.push(item)
  returnArr[await Dialog('是否要退货')]();
}
const router = useRouter()
const goPay = async (item:cart) => {
  itemValue.value = []
  itemValue.value.push(item)
  orderDesc.submit(itemValue.value)
  router.push("orders")

  
}













</script>
<style lang="scss" scoped >
img {
  width: 100%;
}

.item {
  .ctBox {
    display: flex;
    justify-content: space-between;
    margin-top: 8px;

    .pic {
      flex: 0 0 20vw;
      width: 60px;
      height: 60px;

    }

    .desc {
      flex: 0 0 65vw;

      .status {
        text-align: right;
        color: #ff6900;
        padding: 0 10px;
        box-sizing: border-box;
      }

      .name {
        margin: 5px 0;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 1;
        display: -webkit-box;
        overflow: hidden;
      }

      .price {
        margin: 5px 0;

        color: #999;
      }

      .number {
        margin: 5px 0;

        color: #999;
      }
    }

  }

  .ctBtn {
    width: 100vw;
    display: flex;
    flex-direction: row-reverse;

    span {
      width: 120px;
      height: 40px;
      text-align: center;
      line-height: 40px;
      margin: 10px;
    }

    .liji {
      border: 1px solid rgba(255, 28, 17, 0.3);
      color: orange;

    }

    .cancel {
      border: 1px solid rgba(17, 27, 37, 0.3);
      color: #999;

    }
  }
}

.order {
  width: 100vw;
  height: 100vh;
  z-index: 999;
  position: fixed;
  top: 0;
  left: 0;
  background-color: white;
  overflow: auto;
}

.order-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px;
  color: #736666;
  background-color: #f2f2f2;
}

.order-all {
  width: 100vw;
  color: #999;
}

.order-all img {
  display: inline-block;
  width: 110px;
  margin: 0 36vw;
  margin-top: 5vh;
  margin-bottom: 20px;
}
</style>